﻿/*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * 
*     ___                              __                 _       __              __     __       *
*    /   |   _____ ____   ___   _____ /_/____   ____     | |     / /____   _____ / /____/ /_____  *
*   / /| |  / ___// __ \ / _ \ / ___// // __ \ / __ \    | | /| / // __ \ / ___// // __  // ___/  *
*  / ___ | _\_ \ / /_/ //  __// /   / // /_/ // / / /    | |/ |/ // /_/ // /   / // /_/ /_\_ \    *
* /_/  |_|/____// ____/ \___//_/   /_/ \____//_/ /_/     |__/|__/ \____//_/   /_/ \____//____/    *
*              /_/                                                                                *
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace Worlds.Application.Helpers
{
    /// <summary>
    /// XML Helper, helps us save and load XML files.
    /// </summary>
    public static class XMLHelper
    {
        /// <summary>
        /// Saves an object to an XML file.
        /// </summary>
        /// <param name="XMLPath">The path to the file.</param>
        /// <param name="XMLType">The type of object we are saving.</param>
        /// <param name="XMLData">The object we are saving.</param>
        public static void save(String XMLPath, Type XMLType, Object XMLData)
        {
            /* Check that the XMLPath is not empty. */
            if (XMLPath == null || XMLPath == "")
                throw new NullReferenceException("Error saving XML file, reason: The path was empty.");

            /* Check that the XMLType is not null. */
            if (XMLType == null)
                throw new NullReferenceException("Error saving XML file, reason: The object type was empty.");

            /* Check that the XMLData is not null. */
            if (XMLData == null)
                throw new NullReferenceException("Error saving XML file, reason: The object data was empty.");

            /* Create our StreamWriter and XML serializer now. */
            StreamWriter fs = new StreamWriter(XMLPath, false);
            XmlSerializer xmlFile = new XmlSerializer(XMLType);
            
            /* Now serialize and save the file. */
            xmlFile.Serialize(fs.BaseStream, XMLData);

            /* Finally clean up what we were using. */
            fs.Close();
            fs = null;
            xmlFile = null;
            GC.Collect();
        }

        /// <summary>
        /// Loads an object from a XML file.
        /// </summary>
        /// <param name="XMLPath">The path to the file.</param>
        /// <param name="XMLType">The type of object we are loading.</param>
        /// <returns>The object from the XML file, Null if unable to deserialze.</returns>
        public static Object load(String XMLPath, Type XMLType)
        {
            /* Check that the XMLPath is not empty. */
            if (XMLPath == null || XMLPath == "")
                throw new NullReferenceException("Error saving XML file, reason: The path was empty.");

            /* Check that the XMLType is not null. */
            if (XMLType == null)
                throw new NullReferenceException("Error saving XML file, reason: The object type was empty.");

            /* Create our StreamWriter, XML reader, and XML serializer now. */
            StreamWriter fs = new StreamWriter(XMLPath, false);
            XmlSerializer xmlFile = new XmlSerializer(XMLType);
            XmlReader xmlReader = XmlReader.Create(fs.BaseStream);
            Object XMLData = null;

            /* Now dserialize and load the file. */
            if (xmlFile.CanDeserialize(xmlReader))
                XMLData = xmlFile.Deserialize(xmlReader);

            /* Finally clean up what we were using. */
            fs.Close();
            fs = null;
            xmlFile = null;
            xmlReader.Close();
            xmlReader = null;
            GC.Collect();

            /* Now return the object. */
            return XMLData;
        }
    }
}
